package com.ymttest.database.sqlwapper.sqdistributor;

import java.util.HashMap;
import java.util.List;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.sqdistributor.DistributorConsumerRelation;
import com.ymttest.utils.YMTDateUtil;

public class DistributorConsumerRelationWrapper {
	
	SqlManager manager;

	public DistributorConsumerRelationWrapper() {
		manager = new SqlManager("sqdistributor");
	}

	@SuppressWarnings("unchecked")
	public DistributorConsumerRelation selectByPrimaryKey(Long id) {

		DistributorConsumerRelation distributorConsumerRelation = null;
		List<DistributorConsumerRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorConsumerRelationMapper.selectByPrimaryKey",
						id);
		manager.commit();

		if (result.size() > 0) {
			distributorConsumerRelation = result.get(0);
		}

		return distributorConsumerRelation;
	}
	
	@SuppressWarnings("unchecked")
	public DistributorConsumerRelation selectByMerchantIdAndUserId(
			Long merchantId, Long userId) {

		DistributorConsumerRelation distributorConsumerRelation = null;

		HashMap<String, Long> map = new HashMap<>();

		map.put("merchantId", merchantId);
		map.put("userId", userId);

		List<DistributorConsumerRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorConsumerRelationMapper.selectByMerchantIdAndUserId",
						map);
		manager.commit();

		if (result.size() > 0) {
			distributorConsumerRelation = result.get(0);
		}

		return distributorConsumerRelation;
	}
	
	@SuppressWarnings("unchecked")
	public DistributorConsumerRelation selectByMerchantIdAndDistributorId(
			Long merchantId, Long distributorId) {

		DistributorConsumerRelation distributorConsumerRelation = null;

		HashMap<String, Long> map = new HashMap<>();

		map.put("merchantId", merchantId);
		map.put("distributorId", distributorId);

		List<DistributorConsumerRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorConsumerRelationMapper.selectByMerchantIdAndDistributorId",
						map);
		manager.commit();

		if (result.size() > 0) {
			distributorConsumerRelation = result.get(0);
		}

		return distributorConsumerRelation;
	}
	
	public int updateDeleteToFalse(Long id) {

		int result = manager
				.update("com.ymttest.database.mapping.sqdistributor.DistributorConsumerRelationMapper.updateDeleteToFalse",
						id);
		manager.commit();

		return result;
	}
	
	public int updateDeleteStatusByMerchantIdAndUserId(Long merchantId, Long userId, Boolean isDeleted) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("merchant_id", merchantId);
		map.put("user_id", userId);
		map.put("is_deleted", isDeleted);
		
		int result = manager
				.update("com.ymttest.database.mapping.sqdistributor.DistributorConsumerRelationMapper.updateDeleteStatusByMerchantIdAndUserId",
						map);
		manager.commit();

		return result;
	}
	
	public int updateExpirationDate(Long userId, Long distributorId) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("distributor_id", distributorId);
		map.put("user_id", userId);
		map.put("expiration_date", YMTDateUtil.getBeforeOrNextDate(-1));
		
		int result = manager
				.update("com.ymttest.database.mapping.sqdistributor.DistributorConsumerRelationMapper.updateExpirationDate",
						map);
		manager.commit();

		return result;
	}
	
	public static void main(String[] args) {
		
		DistributorConsumerRelationWrapper test = new DistributorConsumerRelationWrapper();
		
//		SellerLevelHistory sellerLevelHistory = new SellerLevelHistory();
//		
//		sellerLevelHistory.setDDate(YMTDateUtil.getDate());
//		sellerLevelHistory.setSellerID(24635724);
//		sellerLevelHistory.setUpdateTime(YMTDateUtil.getDate());
//		sellerLevelHistory.setUnconfirm_amount(BigDecimal.TEN);
//		
		System.out.println(test.updateExpirationDate(25911597L, 25911327L));
	}
}
